package com.atguigu.sort;

import java.util.Arrays;

public class MergeSort {
    public static void main(String[] args) {
        comparator();
    }
    public static void process(int[] a,int L,int R){
        if(L==R)return;
        int mid=L+((R-L)>>1);
        process(a,L,mid);
        process(a,mid+1,R);
        merge(a,L,mid,R);
    }
    public static void merge(int[] a,int L,int mid,int R){
        int p1=L;
        int p2=mid+1;
        int i=0;
        int[] help=new int[R-L+1];
        while(p1<=mid && p2<=R){
            help[i++]=a[p1]<=a[p2]?a[p1++]:a[p2++];
        }
        while(p1<=mid){
            help[i++]=a[p1++];
        }
        while(p2<=R){
            help[i++]=a[p2++];
        }
        for(i=0;i<help.length;i++){
            a[L+i]=help[i];
        }
    }
    //比较器
    public static void comparator() {
        int[] testArray = new int[100];
        for (int j = 0; j < testArray.length; j++) {
            testArray[j] = (int) (Math.random() * 10000 + 1000);
        }
        int[] newArray = Arrays.copyOf(testArray, 100);
        process(newArray, 0, newArray.length - 1);
        Arrays.sort(testArray);
        for (int k = 0; k < 100; k++) {
            if (newArray[k] == testArray[k]) {
                System.out.print("yes ");
            } else {
                System.out.print("fuck ");
            }
        }
        System.out.println();
        for (int o = 0; o < 100; o++) {
            System.out.print(newArray[o] + " ");
        }
        System.out.println();
        for (int o = 0; o < 100; o++) {
            System.out.print(testArray[o] + " ");
        }
    }
}
